﻿using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ProcecureMaterialBusiness : BaseBusiness<PB_ProcecureMaterial>, IPB_ProcecureMaterialBusiness, ITransientDependency
    {
        public PB_ProcecureMaterialBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_ProcecureMaterial>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ProcecureMaterial>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ProcecureMaterial, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_ProcecureMaterial> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_ProcecureMaterial data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_ProcecureMaterial data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<List<PB_ProcecureMaterialDTO>> GetDataListAsyncs(string MaterialId)
        {
            Expression<Func<PB_ProcecureMaterial, PB_Material, PB_ProcecureMain, PB_ProcecureMaterialDTO>> select = (a, b, c) => new PB_ProcecureMaterialDTO
            {
                MaterialIdName = b.Name,
                ProcecureIdName = c.Name,
               ProcecureId=c.Code,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_Material>() on a.MaterialId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_ProcecureMain>() on a.ProcecureId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b,c);
            //筛选
            if (!MaterialId.IsNullOrEmpty())
            {
                q = q.Where(w => w.MaterialId == MaterialId);
            }
            var res = await q.ToListAsync();

            return res;
        }

        public  async Task DeleteOtherIdDataAsync(object where)
        {
            await DeleteOtherIdAsync(where);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}